package com.example.restful01.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.restful01.model.Book;
import com.example.restful01.model.Comments;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface CommentsMapper extends BaseMapper<Comments> {
    @Update("UPDATE `comments` \n" +
            "SET `praise` = `praise` + 1 \n" +
            "WHERE `comment_id` = #{commentId};")
    public int addPraise(Integer commentId);
    @Insert("INSERT INTO `comments` (\n" +
            "    `book_id`, \n" +
            "    `book_name`, \n" +
            "    `reader_id`, \n" +
            "    `reader_name`, \n" +
            "    `content`, \n" +
            "    `date`, \n" +
            "    `praise`, \n" +
            "    `status`\n" +
            ") \n" +
            "SELECT \n" +
            "    b.id,                     \n" +
            "    b.book_name,                  \n" +
            "    u.reader_id,                   \n" +
            "    u.reader_name,                \n" +
            "    #{content},                     \n" +
            "    NOW(),                        \n" +
            "    0,                            \n" +
            "    1                            \n" +
            "FROM \n" +
            "    `book` b CROSS JOIN `user` u  \n" +
            "WHERE \n" +
            "    b.id = #{bookId} AND           \n" +
            "    u.reader_id = #{readerId};     ")
    public int addComment(Comments comments);
    @Insert("INSERT INTO reports (\n" +
            "    reporter_id,\n" +
            "    reader_id,\n" +
            "    book_id,\n" +
            "    report_date,\n" +
            "    date,\n" +
            "    content,\n" +
            "    reporter_name,\n" +
            "    reader_name,\n" +
            "    status,\n" +
            "    book_name\n" +
            ")\n" +
            "SELECT \n" +
            "    u.reader_id AS reporter_id,\n" +
            "    c.reader_id AS reader_id,\n" +
            "    c.book_id,\n" +
            "    NOW() AS report_date,\n" +
            "    c.date,\n" +
            "    c.content,\n" +
            "    u.reader_name AS reporter_name,\n" +
            "    c.reader_name,\n" +
            "    '审核中' AS status,\n" +
            "    c.book_name\n" +
            "FROM \n" +
            "    comments c\n" +
            "JOIN \n" +
            "    user u ON u.reader_id = #{readerId}\n" +
            "WHERE \n" +
            "    c.comment_id = #{commentId};")
    int reportComment(Comments comments);
}
